cc.Class({
  extends: cc.Component,
  properties: {
    stageLeftBtn: cc.Node,
    stageRigthBtn: cc.Node,
    levelPlayerPanel: cc.Node,
    levelMapPanel: cc.Node,
    leftRedNode: cc.Node,
    rigthRedNode: cc.Node,
    shareBtnNode: cc.Node
  },
  onLoad: function () {
    cc.MainUI = this;
  },
  start: function () {
    this.isLookAd = false;
    console.log("  Start Main ");
    cc.pvz.PlayerData.checkAndResetGameDate();
    cc.pvz.PlayerData.checkAndResetAdData();
    cc.pvz.PlayerData.checkAndResetShare();
    this.initMainUI();
  },
  initMainUI: function () {
    this.stageLevel = cc.pvz.PlayerData.getStageLevel();
    this.curStageLevel = this.stageLevel;
    this.refreshLevelMapShow(this.stageLevel);
    this.refreshLevelEnemyShow(this.stageLevel);
    this.refreshLevelPlayerShow();
    this.refreshPageBtnShow();
    cc.pvz.TAUtils.trackAdUIShow(cc.pvz.GameConfig.AdType.关卡扫荡);
  },
  refreshLevelMapShow: function (e) {
    this.levelMap || (this.levelMap = this.levelMapPanel.getComponent("LevelMap"));
    this.levelMap.initLevelMap(e);
  },
  refreshLevelPlayerShow: function () {
    this.levelPlayer || (this.levelPlayer = this.levelPlayerPanel.getComponent("LevelPlayer"));
    this.levelPlayer.initHeroShow();
  },
  refreshLevelEnemyShow: function (e) {
    this.levelPlayer || (this.levelPlayer = this.levelPlayerPanel.getComponent("LevelPlayer"));
    this.levelPlayer.initEnemyShow(e);
  },
  refreshPageBtnShow: function () {
    if (1 == this.curStageLevel) {
      this.stageLeftBtn.active = false;
      this.stageRigthBtn.active = true;
      this.onCheckPageBtnRedTip(false);
    } else if (this.curStageLevel == cc.pvz.GameConfig.MaxLevel || this.curStageLevel - this.stageLevel == 2) {
      this.stageRigthBtn.active = false;
      this.stageLeftBtn.active = true;
      this.onCheckPageBtnRedTip(true);
    } else {
      this.stageLeftBtn.active = true;
      this.stageRigthBtn.active = true;
      this.onCheckPageBtnRedTip(true);
      this.onCheckPageBtnRedTip(false);
    }
  },
  onCheckPageBtnRedTip: function (e) {
    if (e) {
      for (var t = this.curStageLevel - 1; t > 0; t--) {
        var i = this.stageLevel > t;
        var o = cc.pvz.PlayerData.getLevelBoxData(t);
        var a = cc.JsonControl.getLevelJson(t);
        for (var c = 0; c < o.length; c++) {
          var s = a["wave" + (c + 1)];
          if (0 == o[c]) {
            if (i) {
              return void (this.leftRedNode.active = true);
            }
            if (this.levelMap.maxWave >= s && this.stageLevel == t) {
              return void (this.leftRedNode.active = true);
            }
          }
        }
      }
      this.leftRedNode.active = false;
    } else {
      for (var n = this.curStageLevel + 1; n <= this.stageLevel; n++) {
        var r = this.stageLevel > n;
        var h = cc.pvz.PlayerData.getLevelBoxData(n);
        var p = cc.JsonControl.getLevelJson(n);
        for (var l = 0; l < h.length; l++) {
          var d = p["wave" + (l + 1)];
          if (0 == h[l]) {
            if (r) {
              return void (this.rigthRedNode.active = true);
            }
            if (this.levelMap.maxWave >= d && this.stageLevel == n) {
              return void (this.rigthRedNode.active = true);
            }
          }
        }
      }
      this.rigthRedNode.active = false;
    }
  },
  showMainUI: function () {
    this.node.active = true;
    cc.pvz.utils.fadeInBtn(this.node);
    this.stageLevel = cc.pvz.PlayerData.getStageLevel();
    this.curStageLevel = this.stageLevel;
    this.refreshLevelMapShow(this.stageLevel);
    this.levelPlayerPanel.active = true;
    this.refreshLevelEnemyShow(this.stageLevel);
    this.refreshPageBtnShow();
    cc.pvz.TAUtils.trackAdUIShow(cc.pvz.GameConfig.AdType.关卡扫荡);
    this.setClubBtnVisible(true);
    cc.RedControl.onCheckMissionRed();
    cc.UITool && cc.UITool.isChangeArray && this.refreshLevelPlayerShow();
  },
  onHideLvBoxPanel: function () {
    this.levelMap || (this.levelMap = this.levelMapPanel.getComponent("LevelMap"));
    this.levelMap.onHideBoxRewardPanel();
  },
  hideMainUI: function () {
    this.onHideLvBoxPanel();
    this.node.active = false;
    this.setClubBtnVisible(false);
  },
  setClubBtnVisible: function () {},
  onPopup1st: function () {
    this.onHideLvBoxPanel();
    this.setClubBtnVisible(false);
  },
  onAllClosed: function () {
    this.node.active && this.setClubBtnVisible(true);
  },
  onClickStagePage: function (e, t) {
    if (!this.isLookAd) {
      if (parseInt(t) < 0) {
        this.curStageLevel--;
      } else {
        this.curStageLevel++;
      }
      this.refreshLevelMapShow(this.curStageLevel);
      this.levelPlayerPanel.active = this.stageLevel >= this.curStageLevel;
      this.refreshLevelEnemyShow(this.curStageLevel);
      this.refreshPageBtnShow();
    }
  },
  onClickSet: function () {
    this.isLookAd || cc.popupManager.popup("mainUI", "set", "UISet", {
      ad: false,
      scale: true
    }, 0);
  },
  onClickSign: function () {
    this.isLookAd || cc.popupManager.popup("mainUI", "signUI", "UISign", {
      ad: false,
      scale: true
    });
  },
  onClickTask: function () {
    this.isLookAd || cc.popupManager.popup("mainUI", "TaskUI", "UITask", {
      ad: false,
      scale: false
    });
  },
  onClickFund: function () {
    this.isLookAd || cc.popupManager.popup("mainUI", "FundUI", "UIFund", {
      ad: false,
      scale: false
    });
  },
  onClickOther: function () {
    this.onHideLvBoxPanel();
    cc.popupManager.showToast("暂未开启");
  },
  onClickShareUI: function () {
    cc.popupManager.popup("mainUI", "FenxiangUI", "UIShare", {
      ad: false,
      scale: true
    });
  },
  onClickRankMode: function () {
    cc.popupManager.popup("mainUI", "Rank", "UIRankMode", {
      ad: false,
      scale: false
    }, this.node);
  },
  enterGame2: function () {
    cc.assetManager.loadBundle("game2", function (e, t) {
      if (t) {
        cc.director.loadScene("game2");
        cc.player.game2t = Date.now();
      }
    });
  },
  onClickGame2: function () {
    var e = cc.player.game2t || -1;
    if (Date.now() - e < 18e5) {
      cc.popupManager.popup("mainUI", "ModeTipUI", "UIAdGame2", {
        ad: false,
        scale: false
      }, this);
    } else {
      this.enterGame2();
    }
  },
  test: function () {
    cc.view.emit("canvas-resize");
  }
});